import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tin_flutter/app/res/app_color.dart';
import 'package:tin_flutter/app/res/intl.dart';
import 'package:tin_flutter/app/tools/logger.dart';
import 'package:tin_flutter/ui/activity/main/home/widget/brand_widget.dart';
import 'package:tin_flutter/ui/activity/main/home/widget/hot_product_widget.dart';
import 'package:tin_flutter/ui/activity/main/home/widget/new_product_widget.dart';
import 'package:tin_flutter/ui/activity/main/home/widget/recommend_widget.dart';
import 'package:tin_flutter/ui/activity/main/home/widget/sale_widget.dart';

import '../main_logic.dart';

/// * 作者: Tao
/// * 日期: 2025/11/4
/// * 时间: 16:42
/// * 描述: 首页
class HomePage extends StatefulWidget {
  HomePage({Key? key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<HomePage> {
  late ScrollController _scrollController;
  final state = Get.find<MainLogic>().state;

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.grey,
      body: Obx(() {
        return SingleChildScrollView(
            controller: _scrollController,
            child: Stack(children: [
              Container(
                color: AppColor.red600,
                width: MediaQuery.of(context).size.width,
                height: 200.0,
              ),
              Padding(
                padding: EdgeInsets.all(13.0),
                child: Column(
                  children: [
                    // 轮播
                    _buildBanner(),
                    // 类型
                    _buildType(),
                    // 品牌制造商
                    BrandWidget(),
                    // 秒杀专区
                    if (state.flashPromotion.value != null)
                      SaleWidget(),
                    // 新鲜好物
                    NewProductWidget(),
                    // 人气推荐
                    HotProductWidget(),
                    // 猜你喜欢
                    RecommendWidget(
                      scrollController: _scrollController,
                    )
                  ],
                ),
              )
            ]));
      }),
    );
  }

  /// 轮播组件
  Widget _buildBanner() {
    return Container(
      child: CarouselSlider(
          items: items,
          options: CarouselOptions(
            height: 160.0,
            initialPage: 0,
            autoPlay: true,
            autoPlayInterval: Duration(seconds: 3),
            enlargeCenterPage: true,
          )),
    );
  }

  /// 轮播图片
  List<Widget> get items {
    return state.advertiseList
        .map((item) => Container(
              margin: EdgeInsets.all(5.0),
              child: ClipRRect(
                borderRadius: BorderRadius.all(Radius.circular(5.0)),
                child: Image.network(
                  item.pic,
                  fit: BoxFit.fill,
                ),
              ),
            ))
        .toList();
  }

  /// 类型
  Widget _buildType() {
    return Padding(
      padding: EdgeInsets.only(top: 8.0, bottom: 8.0),
      child: Container(
        decoration: BoxDecoration(
          color: AppColor.white,
          borderRadius: BorderRadius.all(Radius.circular(5.0)),
        ),
        child: Padding(
          padding: EdgeInsets.only(top: 8.0, bottom: 8.0),
          child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround, // 均分
              children: [
                _buildTypeItem(AssetImage('assets/images/c3.png'), Intl().specialTopic),
                _buildTypeItem(AssetImage('assets/images/c5.png'), Intl().wordTopic),
                _buildTypeItem(AssetImage('assets/images/c6.png'), Intl().preferred),
                _buildTypeItem(AssetImage('assets/images/c7.png'), Intl().specialOffer),
              ]),
        ),
      ),
    );
  }

  /// 类型item
  Widget _buildTypeItem(ImageProvider icon, String label) {
    return Expanded(
      child: InkWell(
          splashColor: Colors.grey.withValues(alpha: 0.2), // 点击水波纹颜色
          highlightColor: Colors.grey.withValues(alpha: 0.1), // 高亮颜色
          onTap: () {
            logger('点击了 $label');
          },
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Image(
                image: icon,
                height: 35.0,
                width: 35.0,
              ),
              SizedBox(height: 4.0),
              Text(label, style: TextStyle(fontSize: 12.0)),
            ],
          )),
    );
  }
}
